
Imports System
Imports System.Collections
Imports System.Data.SqlClient
Imports System.Diagnostics
Imports _2bMatesSite.Common

Namespace _2bMatesSite.BO
	''' <summary>
	''' Summary description for GroupEvents.
	''' </summary>
	Public Class GroupEvents
		Public Sub New(ByVal grpID As Integer)
		End Sub

		' Find a groupEvent by it's ID
		' returns null if the groupEvent was not found
		Public Shared Function GetGroupEventByID(ByVal id As Integer) As GroupEvent
			Dim cmd As New SqlCommand("SELECT * FROM GroupEvents WHERE ID=@ID")
			cmd.Parameters.Add("@ID", id)

			Return GetGroupEventByCmd(cmd)
		End Function

		Public Shared Function LoadGroupEvents(ByVal grpID As Integer) As ArrayList
			Dim sql As String = "SELECT * FROM GroupEvents WHERE GroupID = @grpID ORDER BY DateTime DESC"

			Dim cmd As New SqlCommand(sql)
			cmd.Parameters.Add("@grpID", grpID)

			Return ListGroupsByCmd(cmd)
		End Function

		Public Shared Function LoadGroupEvents(ByVal grpID As Integer, ByVal topGroupEvents As Integer) As ArrayList
			Dim sql As String = "SELECT TOP " + topGroupEvents + " * FROM GroupEvents WHERE GroupID = @grpID ORDER BY DateTime DESC"

			Dim cmd As New SqlCommand(sql)
			cmd.Parameters.Add("@grpID", grpID)

			Return ListGroupsByCmd(cmd)
		End Function

		' Retrieve the groupEvent using the given SQL command object
		' return null if no groupEvent found
		' NOTE: asserts that only one groupEvent is selected
		'		for more groupEvents use another method
		Protected Shared Function GetGroupEventByCmd(ByVal cmd As SqlCommand) As GroupEvent
			Dim grpEvent As GroupEvent = Nothing

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				cmd.Connection = conn
				Dim dr As SqlDataReader = cmd.ExecuteReader()

				If dr.Read() Then
					grpEvent = New GroupEvent(dr)
					Debug.Assert(Not dr.Read())
				End If

				dr.Close()
			Finally
				conn.Close()
			End Try

			Return grpEvent
		End Function

		' Retrieve a groupEvents list using the given SQL command object
		' return null if no groupEvents are found
		Protected Shared Function ListGroupsByCmd(ByVal cmd As SqlCommand) As ArrayList
			Dim list As New ArrayList()

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				cmd.Connection = conn
				Dim dr As SqlDataReader = cmd.ExecuteReader()

				While dr.Read()
					Dim grpEvent As New GroupEvent(dr)
					list.Add(grpEvent)
				End While

				dr.Close()
			Finally
				conn.Close()
			End Try

			Return list
		End Function
	End Class
End Namespace






